package com.example.cursor_orm.dao.impl;

import com.example.cursor_orm.dao.RoleDAO;
import com.example.cursor_orm.model.Role;
import com.example.cursor_orm.util.DatabaseUtil;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色DAO实现类
 */
@Repository
public class RoleDAOImpl implements RoleDAO {
    
    @Override
    public int insert(Role role) {
        String sql = "INSERT INTO role (name, description) VALUES (?, ?)";
        return DatabaseUtil.executeUpdate(sql, role.getName(), role.getDescription());
    }
    
    @Override
    public int deleteById(Integer id) {
        String sql = "DELETE FROM role WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, id);
    }
    
    @Override
    public int update(Role role) {
        String sql = "UPDATE role SET name = ?, description = ? WHERE id = ?";
        return DatabaseUtil.executeUpdate(sql, role.getName(), role.getDescription(), role.getId());
    }
    
    @Override
    public Role findById(Integer id) {
        String sql = "SELECT * FROM role WHERE id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, id);
        return mapResultSetToRole(rs);
    }
    
    @Override
    public List<Role> findAll() {
        String sql = "SELECT * FROM role ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToRoleList(rs);
    }
    
    @Override
    public List<Role> findByCondition(String condition) {
        String sql = "SELECT * FROM role WHERE " + condition + " ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToRoleList(rs);
    }
    
    @Override
    public int count() {
        String sql = "SELECT COUNT(*) FROM role";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        try {
            if (rs != null && rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    @Override
    public Role findByName(String name) {
        String sql = "SELECT * FROM role WHERE name = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, name);
        return mapResultSetToRole(rs);
    }
    
    @Override
    public List<Role> findByDescriptionLike(String description) {
        String sql = "SELECT * FROM role WHERE description LIKE ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, "%" + description + "%");
        return mapResultSetToRoleList(rs);
    }
    
    @Override
    public boolean existsByName(String name) {
        String sql = "SELECT COUNT(*) FROM role WHERE name = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, name);
        try {
            if (rs != null && rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    @Override
    public List<Role> findByUserId(Integer userId) {
        String sql = "SELECT r.* FROM role r " +
                    "JOIN user_role ur ON r.id = ur.role_id " +
                   "WHERE ur.user_id = ? ORDER BY r.id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, userId);
        return mapResultSetToRoleList(rs);
    }
    
    /**
     * 将ResultSet映射为Role对象
     */
    private Role mapResultSetToRole(ResultSet rs) {
        try {
            if (rs != null && rs.next()) {
                Role role = new Role();
                role.setId(rs.getInt("id"));
                role.setName(rs.getString("name"));
                role.setDescription(rs.getString("description"));
                return role;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将ResultSet映射为Role对象列表
     */
    private List<Role> mapResultSetToRoleList(ResultSet rs) {
        List<Role> roles = new ArrayList<>();
        try {
            if (rs != null) {
                while (rs.next()) {
                    Role role = new Role();
                    role.setId(rs.getInt("id"));
                    role.setName(rs.getString("name"));
                    role.setDescription(rs.getString("description"));
                    roles.add(role);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return roles;
    }
} 